package com.weigee.ktv.v.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.DimenRes;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;

import com.l.d.app.library.util.ResourceUtil;
import com.weigee.ktv.R;

import skin.support.widget.SkinCompatBackgroundHelper;
import skin.support.widget.SkinCompatSupportable;

/**
 * Created by 林德 on 2017/4/5 14:44.
 * <p/>
 */
public class SimpleCustomButton extends View implements SkinCompatSupportable
{
    private SkinCompatBackgroundHelper mBackgroundTintHelper;
    private Paint mPaint;

    /** 线的宽度 */
    private float m_fLineWidth;
    /** 线的颜色 */
    private int m_iLineColor;
    /** 绘制类型 */
    private int m_iButtonDrawType;

    public SimpleCustomButton(Context context)
    {
        this(context, null);
    }

    public SimpleCustomButton(Context context, @Nullable AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public SimpleCustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        this.init(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public SimpleCustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.init(context, attrs);
        mBackgroundTintHelper = new SkinCompatBackgroundHelper(this);
        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs)
    {
        // 默认属性
        m_fLineWidth = 2F;
        m_iLineColor = ResourceUtil.color(context, android.R.color.white);
        m_iButtonDrawType = 1;
        // 用户设置属性
        if (attrs != null)
        {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SimpleCustomButton);

            m_fLineWidth = ta.getDimension(R.styleable.SimpleCustomButton_button_width, m_fLineWidth);
            m_iLineColor = ta.getColor(R.styleable.SimpleCustomButton_button_color, m_iLineColor);
            m_iButtonDrawType = ta.getInt(R.styleable.SimpleCustomButton_button_draw, m_iButtonDrawType);

            ta.recycle();
        }
    }

    public void setLineWidth(@DimenRes int lineWidth)
    {
        m_fLineWidth = ResourceUtil.dimension(getContext(), lineWidth);
        invalidate();
    }

    public void setLineColor(@ColorRes int lineColor)
    {
        m_iLineColor = ResourceUtil.color(getContext(), lineColor);
        invalidate();
    }

    public void setButtonType(int button)
    {
        m_iButtonDrawType = button;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        final Context context = getContext();
        setBackgroundColor(ResourceUtil.color(context, android.R.color.transparent));

        if (mPaint == null)
        {
            mPaint = new Paint();
            mPaint.setColor(m_iLineColor);
            mPaint.setStrokeWidth(m_fLineWidth);
            mPaint.setAntiAlias(true);
        }

        final int width = getWidth();
        final int height = getHeight();
        // 各数值的含义在自定义属性中查看
        switch (m_iButtonDrawType)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                drawArrow(canvas, width, height);
                break;
            case 5:
                drawSlideMenu(canvas, width, height);
                break;
        }
    }

    /**
     * 绘制菜单（多用于左侧、右侧抽屉菜单）
     *
     * @param canvas 画笔
     * @param width  控件宽度
     * @param height 控件高度
     */
    private void drawSlideMenu(Canvas canvas, int width, int height)
    {
        if (mPaint == null)
        {
            invalidate();
            return;
        }
//        final int centerHeight = height / 2;
//        final int lineHeight_2 = (int) (m_fLineWidth / 2);

        final float x1 = 0;
        final float x2 = width * 1f / 10;
        final float x3 = x2 + m_fLineWidth;
        final float x4 = width;
        final float topY = (m_fLineWidth / 2);
        final int centerY = height / 2;
        final float bottomY = height - topY;

        canvas.drawLine(x1, topY, x2, topY, mPaint);
        canvas.drawLine(x3, topY, x4, topY, mPaint);

        canvas.drawLine(x1, centerY, x2, centerY, mPaint);
        canvas.drawLine(x3, centerY, x4, centerY, mPaint);

        canvas.drawLine(x1, bottomY, x2, bottomY, mPaint);
        canvas.drawLine(x3, bottomY, x4, bottomY, mPaint);
    }

    /**
     * 绘制简易箭头
     *
     * @param canvas 画笔
     * @param width  控件宽度
     * @param height 控件高度
     */

    private void drawArrow(Canvas canvas, int width, int height)
    {
        if (mPaint == null)
        {
            invalidate();
            return;
        }
        final int centerHeight = height / 2;
        final int centerWidth = width / 2;
        switch (m_iButtonDrawType)
        {
            case 1:
            default:
                canvas.drawLine(width, 0, 0, centerHeight, mPaint);
                canvas.drawLine(0, centerHeight, width, height, mPaint);
                break;
            case 2:
                canvas.drawLine(0, height, centerWidth, 0, mPaint);
                canvas.drawLine(centerWidth, 0, width, height, mPaint);
                break;
            case 3:
                canvas.drawLine(0, 0, width, centerHeight, mPaint);
                canvas.drawLine(width, centerHeight, 0, height, mPaint);
                break;
            case 4:
                canvas.drawLine(0, 0, centerWidth, height, mPaint);
                canvas.drawLine(centerWidth, height, width, 0, mPaint);
                break;
        }
    }

    @Override
    public void setBackgroundResource(int resId)
    {
        super.setBackgroundResource(resId);
        if (mBackgroundTintHelper != null)
        {
            mBackgroundTintHelper.onSetBackgroundResource(resId);
        }
    }

    @Override
    public void applySkin()
    {
        if (mBackgroundTintHelper != null)
        {
            mBackgroundTintHelper.applySkin();
        }
    }
}
